/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.base.dao.DAOException;
import com.farmcode.base.web.BaseBacking;
import com.farmcode.console.api.business.custom.ExtPropertyManager;
import com.farmcode.console.api.model.Project;
import com.farmcode.console.api.model.Property;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;

/**
 *
 * @author oscar
 */
public class BaseFARMCodeBacking extends BaseBacking {
    
    public static final String CURRENT_PROJECT = Project.class.getName() + "._CURRENT_PROJECT";
    public static final String FARMCODE_CONTEXT = "com.farmcode.web.generator.FARMCODE_CONTEXT";
    
    private Project project;
    private FARMCodeContext context;
    
    @EJB
    private ExtPropertyManager extPropertyManager;
    
    /**
     * @return the project
     */
    public Project getProject() {
        if(getSession().getValue(CURRENT_PROJECT) != null)
            project = getSession().getValue(CURRENT_PROJECT);
        else
            setProject(new Project());
        return project;
    }

    /**
     * @param project the project to set
     */
    public void setProject(Project project) {
        this.project = project;
        getSession().setValue(CURRENT_PROJECT, project);
    }
    
    protected void newContext() {
        setContext(new FARMCodeContext());
        if (getProject() != null) {
            try {
                
                 Property plugin = extPropertyManager.getPropertyByName(getProject(), FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
                 
                getContext().put(FARMCodeContext.PROJECT_NAME, getProject().getName());
                getContext().put(FARMCodeContext.TEMPLATE_PATH, getProject().getPackageTemplate().getName());
                getContext().put(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER, plugin.getValue());
                getContext().put(FARMCodeContext.DATABASE, getProject().getDatabase().getName());
                getContext().put(FARMCodeContext.LANGUAGE, getProject().getLanguage().getName());
                
                setContext(context);
                
            } catch (DAOException ex) {
                Logger.getLogger(BaseFARMCodeBacking.class.getName()).log(Level.SEVERE, null, ex);
                 showError(ex.getMessage());
            }
        }
    }

    protected FARMCodeContext newContext(List<Property> parameters, List<Property> variables) {
        newContext();
        
        if (getProject() != null) {
        
                if(parameters != null){
                    for (Property property : parameters) {
                        context.put(property.getName(), property.getValue());
                    }
                }
                
                if(variables != null){
                    for (Property property : variables) {
                        context.put(property.getName(), property.getValue());
                    }
                }
                
                if (getProject().getLanguage() != null) {
                    context.put(FARMCodeContext.LANGUAGE, getProject().getLanguage().getName());
                }

                if (getProject().getDatabase() != null) {
                    context.put(FARMCodeContext.DATABASE, getProject().getDatabase().getName());
                }
                
                setContext(context);
        }
        
        return context;

    }

    protected FARMCodeContext getContext() {
        if (getSession().getValue(FARMCODE_CONTEXT) != null) {
            context = getSession().getValue(FARMCODE_CONTEXT);
        } else {
            newContext();
        }
        return context;
    }

    protected void setContext(FARMCodeContext context) {
        this.context = context;
        getSession().setValue(FARMCODE_CONTEXT, context);
    }
    
}
